import 'package:flutter/material.dart';

main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "list demo",
      home: MyHomePage(
        title: "首页",
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool empty = false;
  final _controller = ScrollController();
  final List<String> items = List.generate(20, (i) => "Item ${i + 1}");
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: GestureDetector(
            child: Text(widget.title),
            onTap: () {
              if (!empty) {
                // animateTo动画跳转
                _controller.animateTo(-20,
                    duration: const Duration(seconds: 2), curve: Curves.ease);
              }
            },
          ),
        ),
        body: RefreshIndicator(
          onRefresh: () => Future.delayed(const Duration(seconds: 3)),
          child: empty
              ? ListView(
                  children: [
                    Container(
                      height: 400,
                      color: Colors.blue,
                      child: const Text("no data"),
                    ),
                  ],
                )
              : Scrollbar(
                  controller: _controller,
                  child: ListView.builder(
                    controller: _controller,
                    padding: const EdgeInsets.only(
                        bottom: 80), // 最后一个元素加上padding，避免被浮动按钮挡住
                    itemExtent: 60, // 每个块的大小，使拖动滚动条更平滑
                    itemCount: items.length,
                    itemBuilder: (context, index) {
                      return Dismissible(
                        key: UniqueKey(),
                        confirmDismiss: (direction) async {
                          return showDialog(
                            context: context,
                            builder: (context) {
                              return AlertDialog(
                                icon: Icon(Icons.delete),
                                title: Text('Tips'),
                                content: Text("Are you sure delete this item"),
                                actions: [
                                  ElevatedButton(
                                      onPressed: () =>
                                          Navigator.of(context).pop(false),
                                      child: Text("cancel")),
                                  ElevatedButton(
                                      onPressed: () =>
                                          Navigator.of(context).pop(true),
                                      child: Text("confirm"))
                                ],
                              );
                            },
                          );
                        },
                        onDismissed: (direction) => {
                          setState(
                            () {
                              items.removeAt(index); // 移除项
                            },
                          )
                        },
                        child: Container(
                          alignment: Alignment.center,
                          color: Colors.blue[index % 5 * 100],
                          child: Text(items[index]),
                        ),
                      );
                    },
                  ),
                ),
        ));
  }
}
